package com.ruoyi.publish.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.ExcelImportAdvice;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.publish.mapper.IdPublishMapper;
import com.ruoyi.publish.domain.IdPublish;
import com.ruoyi.publish.service.IIdPublishService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出版单位管理Service业务层处理
 *
 * @author 张泽望
 * @date 2020-11-03
 */
@Service
public class IdPublishServiceImpl implements IIdPublishService {
    private static final Logger log = LoggerFactory.getLogger(IdPublishServiceImpl.class);
    @Autowired
    private IdPublishMapper idPublishMapper;

    /**
     * 查询出版单位管理
     *
     * @param id 出版单位管理ID
     * @return 出版单位管理
     */
    @Override
    public IdPublish selectIdPublishById(Long id) {
        return idPublishMapper.selectIdPublishById(id);
    }

    /**
     * 查询出版单位管理列表
     *
     * @param idPublish 出版单位管理
     * @return 出版单位管理
     */
    @Override
    public List<IdPublish> selectIdPublishList(IdPublish idPublish) {
        return idPublishMapper.selectIdPublishList(idPublish);
    }

    /**
     * 新增出版单位管理
     *
     * @param idPublish 出版单位管理
     * @return 结果
     */
    @Override
    public int insertIdPublish(IdPublish idPublish) {
        idPublish.setCreateTime(DateUtils.getNowDate());
        return idPublishMapper.insertIdPublish(idPublish);
    }

    /**
     * 修改出版单位管理
     *
     * @param idPublish 出版单位管理
     * @return 结果
     */
    @Override
    public int updateIdPublish(IdPublish idPublish) {
        idPublish.setUpdateTime(DateUtils.getNowDate());
        return idPublishMapper.updateIdPublish(idPublish);
    }

    /**
     * 删除出版单位管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteIdPublishByIds(String ids) {
        return idPublishMapper.deleteIdPublishByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除出版单位管理信息
     *
     * @param id 出版单位管理ID
     * @return 结果
     */
    @Override
    public int deleteIdPublishById(Long id) {
        return idPublishMapper.deleteIdPublishById(id);
    }


    /**
     * 导入出版单位
     *
     * @param IdPublishList 用户数据列表
     * @param isUpdateSupport      是否更新支持，如果已存在，则进行更新数据
     * @param operName             操作用户
     * @return 结果
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public String importIdPublish(List<IdPublish> IdPublishList, Boolean isUpdateSupport, String operName, String year) {
        if (StringUtils.isNull(IdPublishList) || IdPublishList.size() == 0) {
            throw new BusinessException("导入案件数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        int orderNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (IdPublish entity : IdPublishList) {
            orderNum++;

            try {

                //保存记录
                entity.setCreateTime(DateUtils.getNowDate());
                entity.setCreateBy(operName);
                int u = idPublishMapper.insertIdPublish(entity);


                // 验证是否保成功
                if (u == 1) {

                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 导入成功");
                } else {
                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getLoginName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + " 导入失败：";
                // failureMsg.append(e.getMessage());
                log.error(msg, e);
            }
            orderNum++;
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }


    /**
     * 根据Id获取数据
     *
     * @param ids
     * @return
     */
    @Override
    public List<IdPublish> selectRecordByIds(String[] ids) {
        return idPublishMapper.selectRecordByIds(ids);
    }
}
